<html>
<head>
	<script language="JavaScript" src="scripts/json2.js"></script>
	<script language="JavaScript" src="scripts/jquery-1.4.2.min.js"></script>
	<script type="text/javascript">
		var STR_UNREADED_LIST = 'unreaded_links';
		var STR_UNREADED_COUNT = 'unreaded_count';
		var STR_UNREADED_OID = 'unreaded_oid';
		var STR_OPT_DELONREAD = 'del_on_read';
		var STR_SHORTCUT = 'shortcut';

		var inListColor = new Array(0,255,0,255);
		var notInListColor = new Array(255,0,0,255);

		var isActualTabUnreaded = false;

		if (!localStorage[STR_UNREADED_COUNT])
			localStorage[STR_UNREADED_COUNT] = 0;

		if (!localStorage[STR_UNREADED_OID])
			localStorage[STR_UNREADED_OID] = 0;

		if (!localStorage[STR_OPT_DELONREAD])
			localStorage[STR_OPT_DELONREAD] = 'true';

		if (!localStorage[STR_SHORTCUT])
			localStorage[STR_SHORTCUT] = 10182;

		var otherBmId = null;
		var lrlBmId = null;

		var unreadedList = {};

		if (localStorage[STR_UNREADED_LIST])
			unreadedList = eval('(' + localStorage[STR_UNREADED_LIST] + ')');

		$(document).ready(function(){
			chrome.bookmarks.getTree(function(bm){
				//get 'other Bookmarks' id
				otherBmId = bm[0].children[1].id;

				chrome.bookmarks.getChildren(otherBmId, function(folders){
					for(var i=0;i<folders.length;i++){
						if(folders[i].title == 'LocalReadItLater'){
							afterLRLFolder(folders[i]);
							break;
						}
					}
					if(lrlBmId == null){
						//create new session folder
						chrome.bookmarks.create({'parentId': otherBmId, 'title': 'LocalReadItLater'},
							function(newFolder) {
								afterLRLFolder(newFolder);
							});
					}
				});
			});
		});

		function afterLRLFolder(folder){
			lrlBmId = folder.id;

			chrome.bookmarks.getChildren(lrlBmId, function(childArr){
				localStorage[STR_UNREADED_COUNT] = childArr.length ? childArr.length : 0;
				updateBadgetText(localStorage[STR_UNREADED_COUNT]);
			});
		}

		function updateBadgetText(text){
			if (text == '0') text = '';

			chrome.browserAction.setBadgeText({
				"text": "" + text
			});
		}

		function addToUnReadedList(tab){
			ifTabExist(tab, function(unreadItem){

				if (unreadItem){
					if(!opt_getDelOnRead())
						delFromUnReadedList(unreadItem);

					return;
				}

				var on = false;
				updateBadgetText('new');
				var intervalId = setInterval(function(){
					updateBadgetText(on?'new':'');
					on = !on;
				}, 500);

				chrome.bookmarks.create({'parentId': lrlBmId,
										 'title': tab.title,
										 'url': tab.url
										},
										function(newBmUnreadItem) {
											localStorage[STR_UNREADED_COUNT]++;

											setTimeout(function(){
												clearInterval(intervalId);
												updateBadgetText(localStorage[STR_UNREADED_COUNT]);

												chrome.tabs.getSelected(null, function(tab){
													ifTabExist(tab, function(unreadItem){});
												});
											}, 3000);
										});
			});
		}

		function delFromUnReadedList(unreadItem){
			chrome.bookmarks.remove(unreadItem.id, function(){
				localStorage[STR_UNREADED_COUNT]--;

				updateBadgetText(localStorage[STR_UNREADED_COUNT]);
				setActualTabUnreaded(false);
			});
		}

		chrome.extension.onConnect.addListener(function(port){
			if(port.name == 'LocalReadLater'){
				port.onMessage.addListener(function(data){
					if (data.command == "addToUnReadedList")
						chrome.tabs.getSelected(null, function(tab){
							addToUnReadedList(tab);
						});
				});
			}
		});

		chrome.extension.onRequest.addListener(function(request, sender, func)
		{
			if(request.type == "shortcut")
				func(localStorage[STR_SHORTCUT]);
		});

		chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo) {
			chrome.tabs.get(tabId, function(tab){
				ifTabExist(tab, function(unreadItem){});
			});
		});

		function ifTabExist(tab, callback){
			setActualTabUnreaded(false);

			var returnItem = null;

			chrome.bookmarks.getChildren(lrlBmId, function(childArr){
				searchBM(childArr, tab.url, function(unreadItem){
					if (unreadItem != null){
						setActualTabUnreaded(true);
					}

					callback(unreadItem);
				});
			});
		}

		function searchBM(childArr, url, callback){
			var childFounded = null;
			$.each(childArr, function(pos, child){
				if (child.url == url){
					childFounded = child;
				}
			});

			callback(childFounded);
		}

		function opt_setDelOnRead(optValue){
			localStorage[STR_OPT_DELONREAD] = optValue;
		}

		function opt_getDelOnRead(){
			return localStorage[STR_OPT_DELONREAD] === 'true';
		}

		function setActualTabUnreaded(value){
			isActualTabUnreaded = value;

			if(isActualTabUnreaded)
				chrome.browserAction.setBadgeBackgroundColor({color:inListColor});
			else
				chrome.browserAction.setBadgeBackgroundColor({color:notInListColor});
		}
	</script>
</head>
<body>

</body>
</html>
